<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.enums.UserType"%>
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.enums.RequestStatus"%>
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.enums.RequestAction"%>
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.enums.UserType" %>
<%@page import="ae.pcfc.etks.online.itworx.ehs.web.util.UtilCommon" %>
<%@page import="java.math.*,java.text.*" %>
<%@ include file="/WEB-INF/view/include.jsp"%>
<%@page	import="ae.pcfc.etks.online.itworx.ehs.web.constants.GlobalConstants"%>
<%
	String type = (String) request.getParameter("type");
	String requestId = (String) request.getParameter("requestId");
	String username = (String) session.getAttribute("userName");
	if (null == type) {
		type = "";
	}
%>
<script>

var contextPath = '<%=request.getContextPath()%>';

function openFnpWindow() {
	window.open(contextPath + "/fnp/fnpMapping.htm", "Fees_Calculations");
}

function validateFees(action){
	if(document.getElementById("fnpFees").value == "")
	{
		document.getElementById("errorDiv").innerHTML = '<spring:message code="error.officer.payment.fees.needed"/>';
		document.body.scrollTop = document.documentElement.scrollTop = 0;
	}else{
		submitForm(action);
	}
}
	function submitForm(action) {
		var isValid = validateCommentsLength();
		 if(!isValid){
				document.body.scrollTop = document.documentElement.scrollTop = 0;
				return;
		 }
		document.getElementById("action").value = action;
		document.forms[0].submit();
	}

	function validateComments(action) {
		var officerComments = document.getElementById("officerComments").value;
		if (null == officerComments || officerComments == "") {
			document.getElementById('errorDiv').innerHTML = '<spring:message code="error.ofcRequestModel.officerComments"/>';
			document.body.scrollTop = document.documentElement.scrollTop = 0;
		}else{
			submitForm(action);
		}
}

function validateCommentsLength(){
	var officerComments = document.getElementById("officerComments").value;
	if(null != officerComments && officerComments != "" && officerComments.length > 500){
		document.getElementById('errorDiv').innerHTML = '<spring:message code="Length.ofcRequestModel.officerComments"/>';
		return false;
	}
	
	return true;
}

function hideShowApproveButton(){
	var status=document.getElementById("status");
	if(status.value == 'Submitted' || status.value == 'Resubmitted' || status.value == 'Inspection_Approved')
	{
		var approveID = document.getElementById("approveButton");
		var approveSpamID = document.getElementById("approveSpan");
		var feesValue = document.getElementById("fnpFees").value;
		if(null != approveID){
			if(null == feesValue || feesValue == "")
			{
				approveID.disabled ='disabled'; // disabled
			}else{
				approveID.disabled  = ''; // enable
				approveID.className ='btn_bg';
				approveSpamID.className ='btn_border';
			}
		}
	}
}

function confirmClose(action){
	$.alerts.okButton='<spring:message code="label.document.delete.confirm.yes"/>';
	$.alerts.cancelButton='<spring:message code="label.document.delete.confirm.no"/>';
	
	jConfirm('<spring:message code="label.request.close.confirm.message"/>','', function(r){
		if(r){
			document.getElementById("action").value = action;
			document.forms[0].submit();
		}else {
			return false;
		}
	});
}

window.onload = function(){
		hideShowApproveButton();
};
</script>

<%
		if (session.getAttribute(GlobalConstants.SUCCESS_PARAM) != null) {
	%>
<script type="text/javascript">
				$(document).ready(
								function() {
									jAlert(
											'<%=session.getAttribute(GlobalConstants.SUCCESS_PARAM)%>','',
											'<spring:message code="msg.success" />');
								});
			</script>
<%
	session.removeAttribute(GlobalConstants.SUCCESS_PARAM);
	}
%>

<form:form modelAttribute="<%=GlobalConstants.OFC_REQUEST_MODEL%>"
	method="POST" action="officer/viewDetails.htm">

	<form:hidden path="status" id="status"/>	
	<div style="color:#ED0000;font-size:12px;margin:0 0 10px 10px" id="globalErrorDiv">
			<spring:bind path="ofcRequestModel.*">
				<c:forEach items="${errors.globalErrors}" var="error">
					<c:out value="${error}" />
					<br />
				</c:forEach>
			</spring:bind>
		</div>
		
	<div style="color:#ED0000;font-size:12px;margin:0 0 10px 10px" id="errorDiv" name="errorDiv" >
		<spring:bind path="ofcRequestModel.*">
			<c:forEach items="${status.errorMessages}" var="error">
				<c:out value="${error}" />
				<br />
			</c:forEach>
		</spring:bind>
	</div>

			<fieldset>
				<legend>
					<spring:message code="label.request.requestDetails" />
				</legend>
			</fieldset>
			<div class="t_form-left">
				<label><spring:message code="label.request.requestId" />:</label>				
				<input  class= "t_form-fld-readonly" value="${ofcRequestModel.serviceName}_${ofcRequestModel.type}_<fmt:formatDate value="${ofcRequestModel.creationDate}" pattern="<%=GlobalConstants.DEFAULT_VIEW_ID_DATE_FORMAT%>" />_${ofcRequestModel.id}" title="${ofcRequestModel.serviceName}_${ofcRequestModel.type}_<fmt:formatDate value="${ofcRequestModel.creationDate}" pattern="<%=GlobalConstants.DEFAULT_VIEW_ID_DATE_FORMAT%>" />_${ofcRequestModel.id}" readonly="readonly"/>
			</div>
			<div class="t_form-right">
				<label><spring:message code="label.request.reqStatus" /></label>
				<input value="<spring:message code="${ofcRequestModel.status}"/>" readonly="readonly" class="t_form-fld-readonly"/>
			</div>
			
			<div class="brk"></div>
			
	<fieldset>
		<legend>
			<spring:message code="label.request.company.nameeng.label" />
		</legend>
	</fieldset>
	<div class="t_form-left">
		<label> <spring:message code="label.request.company.nameeng" />
		</label>
		<form:input path="companyNameEnglish" name="input" type="text"
			readonly="true" class="t_form-fld-readonly" title="${ofcRequestModel.companyNameEnglish}"/>

		<label><spring:message code="label.request.TradeLicenceNo" />
		</label>

		<form:input path="licenseNumber" class="t_form-fld-readonly" readOnly="true" />

		<label><spring:message code="label.request.buisnessUnit" /> </label>
		<form:input path="businessUnit" type="text" readonly="true"
			class="t_form-fld-readonly" title="${ofcRequestModel.businessUnit}"/>

		<label> <spring:message code="label.request.mailMail" /> </label>
		<form:input path="mainEmail" type="text" readonly="true"
			class="t_form-fld-readonly" />

		<label><spring:message code="label.request.requestedBy" /> </label>
		<form:input type="text" class="t_form-fld-readonly" path="requestedBy"
			readOnly="true" />

		<label> <spring:message code="label.request.remarks" /> </label>
		<form:textarea path="remarks" naem="textarea" class="t_address t_form-fld-readonly"
			readOnly="true"></form:textarea>

	</div>
	<!--form-left-end-->

	<div class="t_form-right">
		<label> <spring:message code="label.request.company.namearab" />
		</label>
		<form:input path="companyNameArabic" type="text" readonly="true"
			class="t_form-fld-readonly" title="${ofcRequestModel.companyNameArabic}"/>


		<label><spring:message code="label.request.location" />:</label>
		<form:input path="location" class="t_form-fld-readonly" readonly="true" />

		<label><spring:message code="label.request.bcc" /> </label>
		<form:input path="bcc" type="text" class="t_form-fld-readonly" readOnly="true" />

		<label> <spring:message code="label.request.optionalMail" />
		</label>
		<form:input type="text" class="t_form-fld-readonly" path="optionalMail"
			readOnly="true" />
		<label> <spring:message code="label.request.date" /> </label>
		<form:input path="creationDate" type="text" readonly="true"
			class="t_form-fld-readonly" />
		
		<label><spring:message code="label.request.activity" /> </label>
		<c:choose>
				<c:when test='${requestScope.requestContextAttribute.locale.language eq "ar"}'>
					<form:hidden path="engActivity" id="engActivity" title="${ofcRequestModel.engActivity}"/>
					<form:textarea path="arActivity" type="text" readonly="true"
						class="t_address t_form-fld-readonly" id="arActivity" title="${ofcRequestModel.arActivity}"/>
				</c:when>
				<c:otherwise>
					<form:textarea path="engActivity" type="text" readonly="true"
						class="t_address t_form-fld-readonly" id="engActivity" title="${ofcRequestModel.engActivity}"/>
					<form:hidden path="arActivity" id="arActivity" title="${ofcRequestModel.arActivity}"/>
				</c:otherwise>
			</c:choose>
	</div>
	<!--form right close-->

	<div class="brk"></div>
	<fieldset>
		<legend>
			<spring:message code="label.request.contactPerson.info" />
		</legend>
	</fieldset>
	<div class="t_form-left">
		<label> <spring:message
				code="label.request.contactPerson.name" />:</label>
		<form:input path="contactPersonName" name="input" type="text"
			readOnly="true" maxlength="50" class="t_form-fld-readonly" />


		<label><spring:message
				code="label.request.contactPerson.telephone" />:</label>
		<form:input path="telephone" name="input" type="text"
			class="t_form-fld-readonly" readOnly="true" />


		<label><spring:message code="label.request.faxNo" /> </label>
		<form:input type="text" class="t_form-fld-readonly" path="fax" readOnly="true" />
	</div>
	<!--form-left-end-->

	<div class="t_form-right">
		<label> <spring:message
				code="label.request.contactPerson.mobile" /> </label>
		<form:input path="mobile" name="input" type="text" class="t_form-fld-readonly"
			readOnly="true" />
		<label> <spring:message
				code="label.request.contactPerson.email" />:</label>
		<form:input path="alternativeEmail" name="input" type="text"
			class="t_form-fld-readonly" readOnly="true" />

	</div>
	<!--form right close-->
	<c:choose>
		<c:when test="${ofcRequestModel.type eq 'Amendment'}">
			<div class="brk"></div>
			<div id="amendment">
				<fieldset>
					<div class="brk"></div>
					<legend>
						<spring:message code="label.request.amendmentType" />
					</legend>
				</fieldset>
				<div class="t_form-left">
					<label class="multiple_selection_label"><spring:message
							code="label.request.amendmentType" />:</label>
					<c:if test="${not empty  ofcRequestModel.amendmentTypesAsArray}" >
						<br />
						<table style="width:100%;display:block; color: #666666;font-size: 12px;">
							<c:forEach items="${ofcRequestModel.amendmentTypesAsArray}" var="type">
							<tr style="width: 100%">
								<td style='width:5%'>
									<img src="resources/images/common/bullet.gif" width="11px" height="8px"/>
								</td>
								<td style='width:95%'>
									<c:set var="typeStr" >${type.propertyName}</c:set>
									<c:set var="underscoredType" value='<%=UtilCommon.underscore((String)pageContext.getAttribute("typeStr")) %>' />
									<spring:message code="${underscoredType}" />
								</td>
							</tr>
							</c:forEach>
						</table>
					</c:if>
					<form:select name="select" class="multiple,multiple_disabled" path="amendmentType"
						multiple="true" disabled="true" style="display:none">
						<form:option value="0" label="Select" />
						<c:forEach items="${ofcRequestModel.amendmentTypes}" var="optionType">
							<c:set var="typeStr" value='${optionType.propertyName}' />
							<c:set var="underscoredType" value='<%=UtilCommon.underscore((String)pageContext.getAttribute("typeStr")) %>' />
							<form:option value="${optionType.propertyName}" >
								<spring:message code="${underscoredType}" />
							</form:option>
						</c:forEach>
					</form:select>
					<form:hidden path="amendmentType"/>
				</div>
			</div>
		</c:when>
	</c:choose>

	<c:choose>
		<c:when test="${(null != ofcRequestModel.imsLogId) and (not empty ofcRequestModel.imsLogId)}">
			<div class="brk"></div>
			<div id="renewal">
				<fieldset>
					<legend>
						<spring:message code="label.request.imsLogId" />
					</legend>
				</fieldset>
				<div class="t_form-left">
					<label> <spring:message code="label.request.imsLogId" />:</label>
					<form:input type="text" cssClass="t_form-fld-readonly" path="imsLogId"
						readOnly="true" />
				</div>
			</div>
		</c:when>
	</c:choose>

	<div class="brk"></div>
	<c:if
		test="${null != ofcRequestModel.documents or not empty ofcRequestModel.documents}">
		<table id="documents" class="grdView">
			<caption>
				<spring:message code="label.request.documents" />
			</caption>
			<colgroup>
				<col width="35%" />
				<col width="65%" />
			</colgroup>
			<tr>
				<th><spring:message code="label.request.document.type" /></th>
				<th><spring:message code="label.request.document.uploadedFile" />
				</th>
			</tr>
			<c:forEach var="document" items="${ofcRequestModel.documents}"
				varStatus="status">
				<tr id="documentRow" ${status.index % 2 == 0 ? 'class="grdAltRow"' : ''}>
					<td><c:choose>
							<c:when
								test='${requestScope.requestContextAttribute.locale.language eq "ar"}'>
								<form:hidden
									path="documents[${status.index}].documentType.arabicName"/>${document.documentType.arabicName}
							</c:when>
							<c:otherwise>
								<form:hidden
									path="documents[${status.index}].documentType.englishName"/>${document.documentType.englishName}
							</c:otherwise>
						</c:choose> <form:hidden path="documents[${status.index}].documentType.id" />
						<form:hidden path="documents[${status.index}].id" /> <form:hidden
							path="documents[${status.index}].documentLocation" /></td>
					<td><a href="common/download.htm?fileId=${document.id}">${document.documentName}</a>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	<!--form right close-->
	<div class="brk"></div>

	<form:hidden path="type" />
	<c:if
		test="${(ofcRequestModel.status eq 'Submitted') or (ofcRequestModel.status eq 'Additional_Payment_Done') or (ofcRequestModel.status eq 'Payment_Done') or (ofcRequestModel.status eq 'Resubmitted') or (ofcRequestModel.status eq 'Inspection_Approved')}">
		<fieldset>
			<legend>
				<spring:message code="label.request.history.comments" />
			</legend>
		</fieldset>
		<div class="t_form-left">
			<label><spring:message code="label.request.comments" /> </label>
			<form:textarea rows="10" cols="50" path="officerComments"></form:textarea>
		</div>

		<div class="brk"></div>
	</c:if>
	
	<c:set var="officerUserType" value="<%=UserType.Officer %>"/>
	<table id="Table1" class="grdView">
		<caption>
			<spring:message code="label.request.history.action" />
		</caption>
		<colgroup>
			<col width="18%" />
			<col width="23%" />
			<col width="23%" />
			<col width="39%" />
		</colgroup>
		<tr>
			<th><spring:message code="label.request.history.actionTaken" />
			</th>
			<th><spring:message code="label.request.history.username" /></th>
			<th><spring:message code="label.request.history.dateTime" /></th>
			<th><spring:message code="label.request.history.comments" /></th>
		</tr>
		
		<c:forEach items="${ofcRequestModel.ofcHistory}" var="history" varStatus="statusIndex">
			<tr ${statusIndex.index % 2 == 0 ? 'class="grdAltRow"' : ''}>
				<td><spring:message code="${history.status}" />
				</td>
				<td>
					<c:choose>
						<c:when test="${history.executer == officerUserType}" >
							${history.officerId}
						</c:when>
						<c:otherwise>
							${history.userId}
						</c:otherwise>
					</c:choose>
				</td>
				<td><fmt:formatDate value="${history.processingDate}" pattern="<%=GlobalConstants.DEFAULT_VIEW_DATE_FORMAT%>"/> </td>
				<td>${history.officerComments}</td>
			</tr>
		</c:forEach>

	</table>

	<form:hidden path="id" />
	<form:hidden path="processId" />
	<form:hidden path="fnpDetails" />
	<form:hidden path="trakheesId" />
	<input type="hidden" name="action" id="action" />

	<div class="brk"></div>
	
			<c:if
				test="${(null != ofcRequestModel.comments) and (not empty ofcRequestModel.comments)}">
				<table id="Table2" class="grdView">
					<caption>
						<spring:message code="label.request.nonComplianceComment" />
					</caption>
					<colgroup>
						<col width="6%" />
						<col width="30%" />
						<col width="8%" />
						<col width="8%" />
						<col width="13%" />
						<col width="11%" />
						<col width="24%" />
					</colgroup>
					<tr>
						<th><spring:message code="label.request.nonComplianceComment.id"/></th>
						<th><spring:message code="label.request.nonComplianceComment.comment"/></th>
						<th><spring:message code="label.request.nonComplianceComment.severity"/></th>
						<th><spring:message code="label.request.nonComplianceComment.entryDate"/><br/><spring:message code="label.request.dateFormat" /></th>
						<th><spring:message code="label.request.nonComplianceComment.inspectorName"/></th>
						<th><spring:message code="label.request.nonComplianceComment.section"/></th>
						<th><spring:message code="label.request.nonComplianceComment.reply"/></th>
					</tr>
					<c:forEach items="${ofcRequestModel.comments}" var="comment" varStatus="loopStatus">
						<tr ${loopStatus.index % 2 == 0 ? 'class="grdAltRow"' : ''}>
							<td>${comment.id}</td>
							<td>${comment.commentDetails}</td>
							<td>${comment.imsSeverity}</td>
							<td><fmt:formatDate value="${comment.commentDate}" pattern="<%=GlobalConstants.DEFAULT_VIEW_DATE_FORMAT%>"/></td>
							<td>${comment.imsInspectorName}</td>
							<td>${comment.imsDeptId}</td>
							<td>${comment.reply}</td>
						</tr>
					</c:forEach>
				</table>

			</c:if>
	
	<form:hidden path="userId" />
	
	<c:if
			test="${(null != ofcRequestModel.fnpList) || (not empty ofcRequestModel.fnpList)}">
			<div class="brk"></div>
			<fieldset>
                    <legend><spring:message code="label.request.fees" /></legend>
 			</fieldset>
 			<div class="brk"></div>
			<c:forEach items="${ofcRequestModel.fnpList}" var="fnp" varStatus="fnpVarStatus">
				<c:set var="fees" scope="request" value="${fnp.fees}"/>
					<%
						BigDecimal formattedFees = new BigDecimal((Float)request.getAttribute("fees"));
						String decimalFormat = new DecimalFormat(GlobalConstants.FEES_FORMAT).format(formattedFees);
					%>
				<c:choose>
					<c:when test="${fnpVarStatus.index == 0}">
							<div class="t_form-left">
								<label><spring:message code="label.request.fees" /><span class="t_star"></span>:</label>
								<input type="text" class="t_form-fld-readonly"  disabled="disabled" value="<%=decimalFormat%>"/>
								<span class="fees_currency"><spring:message code="fnp.fees.currency" /></span>
							</div>
							<div class="t_form-right">
								<label><spring:message code="label.request.paymentAdviceNo" /></label>
								<input type="text" class="t_form-fld-readonly" disabled="disabled" value="${fnp.paymentNumber}"/>
							</div>
						</c:when>
						<c:otherwise>
							<div class="t_form-left">
								<label><spring:message code="label.request.additionalFees" /><span class="t_star"></span>:</label>
								<input type="text" class="t_form-fld-readonly"  disabled="disabled" value="<%=decimalFormat%>"/>
								<span class="fees_currency"><spring:message code="fnp.fees.currency" /></span>
							</div>
							<div class="t_form-right">
								<label><spring:message code="label.request.addPaymentAdviceNo" /></label>
								<input type="text" class="t_form-fld-readonly"  disabled="disabled" value="${fnp.paymentNumber}"/>
							</div>
						</c:otherwise>
					</c:choose>
							<div class="brk"></div>
			</c:forEach>
		</c:if>
		<div class="brk"></div>
		
	
		<c:if test="${ofcRequestModel.status == 'Submitted' || ofcRequestModel.status == 'Resubmitted' || ofcRequestModel.status == 'Inspection_Approved'}">
		
		<div class="t_form-left" id="calculate_fees">
		
		<c:choose>
			<c:when test="${ofcRequestModel.status == 'Inspection_Approved'}">
							
			<label><spring:message code="label.request.additional.fees" /><span
						class="t_star">*</span>:</label> 	
			</c:when>
			<c:otherwise>
				<label><spring:message code="label.request.fees" /><span
						class="t_star">*</span>:</label> 
			</c:otherwise>
		</c:choose>
			
				<input type="text" id="fnpFees" name="fnpFees"  readonly="readonly"/><span class="fees_currency"><spring:message code="fnp.fees.currency" /></span>
					
			<!-- GetFees Button -->
			<a class="iframe" href="fnp/fnpMapping.htm?userId=${ofcRequestModel.userId}&requestId=${ofcRequestModel.id}">
				<span class="btn_border">
					<button type="button" class="btn_bg"><spring:message code="label.request.button.getFees" />
					</button>
				</span>
			</a>
		</div>
		</c:if>
		
	<c:choose>
	<c:when test="${ofcRequestModel.status == 'Submitted' || ofcRequestModel.status == 'Resubmitted'}">
			<div class="brk"></div>

			<div id="fm-submit">
				<span class="btn_border btn_border_disabled" id = "approveSpan"> 
					<button type="button" class="btn_bg btn_bg_disabled" onclick="validateFees('approve')" id="approveButton">
						<spring:message code="label.request.button.approve" />
					</button> 
				</span>
				<span class="btn_border">
					<button type="button" class="btn_bg"
						onclick="validateComments('return_for_updates');">
						<spring:message code="label.request.button.returnForUpdate" />
					</button> </span> <span class="btn_border">
					<button type="button" class="btn_bg"
						onclick="validateComments('reject');">
						<spring:message code="label.request.button.reject" />
					</button>
				</span> 
			</div>
		</c:when>
		<c:when test="${ofcRequestModel.status == 'Inspection_Approved'}">
			<div id="fm-submit">
				<span class="btn_border btn_border_disabled" id = "approveSpan">
					<button type="button" class="btn_bg btn_bg_disabled"
						onclick="validateFees('additional_fees_required');" id="approveButton">
						<spring:message code="label.request.button.additionalFees" />
					</button> </span> <span class="btn_border">
					<button type="button" class="btn_bg" onclick="confirmClose('close');">
						<spring:message code="label.request.button.close" />
					</button> </span>
			</div>
		</c:when>
		<c:when test="${ofcRequestModel.status == 'Payment_Done'}">
			<div id="fm-submit">
				<span class="btn_border">
					<button type="button" class="btn_bg"
						onclick="submitForm('payment_confirmed');">
						<spring:message code="label.request.button.paymentConfirm" />
					</button> 
				</span>
			</div>
		</c:when>
		<c:when test="${ofcRequestModel.status == 'Additional_Payment_Done'}">
			<div id="fm-submit">
				<span class="btn_border">
					<button type="button" class="btn_bg"
						onclick="submitForm('additional_payment_confirmed');">
						<spring:message code="label.request.button.confirmAdditionalFees" />
					</button> 
				</span>
			</div>
		</c:when>
	</c:choose>

</form:form>
